<template>
  <div class="app-container" v-loading="loading">
    <el-descriptions class="app-container" title="公司信息">
      <el-descriptions-item label="父公司账号：">
        {{ data?.parentCompanyAccount ?? '无' }}
      </el-descriptions-item>
      <el-descriptions-item label="父公司名称：">
        {{ data?.parentName ?? '无' }}
      </el-descriptions-item>
      <el-descriptions-item label="公司账号：">{{ data?.companyAccount }}</el-descriptions-item>
      <el-descriptions-item label="公司名称：">{{ data?.name }}</el-descriptions-item>
      <el-descriptions-item label="部门名称：">{{ data?.deptName }}</el-descriptions-item>
      <el-descriptions-item label="状态：">
        <dict-tag
          style="display: inline-block"
          :options="sys_common_staus"
          :value="data?.enabled"
        />
      </el-descriptions-item>
      <el-descriptions-item label="公司地址：">{{ data?.address }}</el-descriptions-item>
      <el-descriptions-item label="合作模式：">
        <dict-tag
          style="display: inline-block"
          :options="sys_cooperation"
          :value="data?.cooperationMode"
        />
      </el-descriptions-item>
      <el-descriptions-item label="营业执照有效期：">
        {{
          `${data?.bizLicenceBeginTime || ''}~ ${
            data?.longTerm == 1 ? '长期' : data?.bizLicenceEndTime || ''
          }`
        }}
      </el-descriptions-item>
      <el-descriptions-item label="公司负责人：">{{ data?.contacts }}</el-descriptions-item>
      <el-descriptions-item label="负责人联系方式：：">{{ data?.mobile }}</el-descriptions-item>
      <el-descriptions-item label="" />
      <el-descriptions-item label="法人：">{{ data?.legalPerson }}</el-descriptions-item>
      <el-descriptions-item label="法人联系方式：">{{ data?.lglPsnMobile }}</el-descriptions-item>
      <el-descriptions-item label="操作人：">{{ data?.updateName }}</el-descriptions-item>
      <el-descriptions-item label="合同开始时间：">
        {{ data?.contractBeginDate }}
      </el-descriptions-item>
      <el-descriptions-item label="合同到期时间：">
        {{ data?.contractEndDate }}
      </el-descriptions-item>
      <el-descriptions-item label="操作时间：">{{ data?.createTime }}</el-descriptions-item>
      <el-descriptions-item label="经营许可证：">
        <el-image
          :src="data?.manageLicence"
          :zoom-rate="1.2"
          :max-scale="7"
          :min-scale="0.2"
          :preview-src-list="[data?.manageLicence]"
          fit="cover"
        />
      </el-descriptions-item>
      <el-descriptions-item label="" />
      <el-descriptions-item label="营业执照：">
        <el-image
          :src="data?.bizLicence"
          :zoom-rate="1.2"
          :max-scale="7"
          :min-scale="0.2"
          :preview-src-list="[data?.bizLicence]"
          fit="cover"
        />
      </el-descriptions-item>
    </el-descriptions>

    <el-divider />
    <el-descriptions title="支付设置">
      <el-descriptions-item class="app-container" label="支付方式：">
        <dict-tag style="display: inline-block" :options="sys_payment" :value="data.paymentCode" />
      </el-descriptions-item>
    </el-descriptions>

    <div class="justify-space-evenly">
      <el-descriptions title="微信账号设置" :column="1">
        <el-descriptions-item label="微信支付商户号：">
          {{ payConfig?.wxMchId }}
        </el-descriptions-item>

        <el-descriptions-item label="API密钥：">{{ payConfig?.apiSecret }}</el-descriptions-item>

        <el-descriptions-item label="证书路径：">
          <el-icon
            v-if="payConfig?.certPath"
            @click="download('证书', payConfig?.certPath)"
            color="#409EFC"
            class="no-inherit"
          >
            <DocumentCopy />
          </el-icon>
        </el-descriptions-item>
      </el-descriptions>

      <el-descriptions title="支付宝账号设置" :column="1">
        <el-descriptions-item label="支付宝应用ID：">
          {{ payConfig?.appId }}
        </el-descriptions-item>

        <el-descriptions-item label="应用公钥证书：">
          <el-icon
            v-if="payConfig?.appPrivateKeyFile"
            @click="download('应用公钥证书', payConfig?.appPrivateKeyFile)"
            color="#409EFC"
            class="no-inherit"
          >
            <DocumentCopy />
          </el-icon>
        </el-descriptions-item>

        <el-descriptions-item label="支付宝公钥证书：">
          <el-icon
            v-if="payConfig?.aliPublicKey"
            @click="download('支付宝公钥证书', payConfig?.aliPublicKey)"
            color="#409EFC"
            class="no-inherit"
          >
            <DocumentCopy />
          </el-icon>
        </el-descriptions-item>

        <el-descriptions-item label="应用私钥：">
          {{ payConfig?.appPrivateKey }}
        </el-descriptions-item>
      </el-descriptions>
    </div>

    <div class="flex-center">
      <el-button @click="close">关 闭</el-button>
    </div>
  </div>
</template>

<script setup name="CompanyDetail">
import { getCompanyInfo } from '@/api/organization/company'
const route = useRoute()
const { proxy } = getCurrentInstance()

const { sys_payment, sys_cooperation, sys_common_staus } = proxy.useDict(
  'sys_payment',
  'sys_cooperation',
  'sys_common_staus'
)

const data = ref({})
const payConfig = ref({})
const loading = ref(false)

const download = (filename, url) => {
  let a = document.createElement('a')
  a.style = 'display: none' // 创建一个隐藏的a标签
  a.download = filename
  a.href = url
  a.target = '_blank'
  document.body.appendChild(a)
  a.click() // 触发a标签的click事件
  document.body.removeChild(a)
}

// 获取公司详情
watch(
  () => route.query.id,
  async (id) => {
    if (id) {
      loading.value = true
      try {
        const r = await getCompanyInfo(id)
        if (r?.data) {
          const { companys, alipayInfo = {}, wxpayInfo = {} } = r?.data ?? {}
          data.value = { ...companys }
          payConfig.value = { ...alipayInfo, ...wxpayInfo }
        }
      } finally {
        loading.value = false
      }
    }
  },
  {
    immediate: true
  }
)

function close() {
  proxy.$tab.closePage()
}
</script>

<style lang="scss" scoped>
.el-image {
  width: 200px;
}

.no-inherit {
  font-size: 50px;
  cursor: pointer;
}
</style>
